﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QuTouTiaoAssist.Entity;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmMyPupils : FrmBase
    {
        public FrmMyPupils()
        {
            InitializeComponent();
            ClearControl();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (!base.CheckLogin())
                return;

            ClearControl();
            new Thread(() =>
            {
                try
                {
                    if (!CheckLogin())
                        return;
                    var info = QttHelper.GetPupilIncomes(base.MemeberToken);
                    base.InvokeCrossThread<List<QttPupilIncomeInfo>>(Info2Controls, info);
                }
                catch (Exception ex) 
                {
                    Console.WriteLine(ex.Message);
                }
            }).Start();
        }

        private void ClearControl()
        {
            this.listView1.Items.Clear();
            this.label1.Text = "";
        }

        private void Info2Controls(List<QttPupilIncomeInfo> list)
        {
            int index = 0;

            int pupilNum = 0;
            if (list != null)
            {
                foreach (QttPupilIncomeInfo info in list)
                {
                    this.listView1.Items.Add(new ListViewItem(new string[]
                    {
                        (++index).ToString(),
                        info.Mobile,
                        info.MemberId,
                        info.Coin.ToString()
                    }));
                }
                pupilNum = list.Count;
            }

            this.label1.Text = "徒弟数：" + pupilNum;
        }
    }
}
